package cn.abellflower.dzfp;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author gbzdyf@126.com
 * @Date 2020/10/19 8:47 下午
 */
public class SchedulerService extends BaseService {

    private MessageConsumer consumer;

    private ArrayBlockingQueue[] schedulerQueue;

    private int schedulerThreadNumber = 10;

    public SchedulerService() {
        schedulerQueue = new ArrayBlockingQueue[schedulerThreadNumber];
        for (int i = 0; i < schedulerThreadNumber; i++) {
            schedulerQueue[i] = new ArrayBlockingQueue(1);
        }

        consumer = createConsumer();
        consumer.start();
    }

    public static void main(String[] args) {
        SchedulerService schedulerService = new SchedulerService();
    }

    @Override
    public void onMessage(String msg) {
        System.out.println( msg + "SchedulerService");
        int hashCode = msg.hashCode() % schedulerThreadNumber;
        try {
            if (!schedulerQueue[hashCode].offer(1, 3, TimeUnit.SECONDS)) {
                System.out.println("--------------添加失败");
            } else {
                System.out.println("添加成功");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException();
        }
    }
}
